package fr.cnam.projetSiw.client.core;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootPanel;

import fr.cnam.projetSiw.client.core.displayArea.DisplayArea;
import fr.cnam.projetSiw.client.core.mapper.BotLeftContentMapper;
import fr.cnam.projetSiw.client.core.mapper.BotMidContentMapper;
import fr.cnam.projetSiw.client.core.mapper.BotRightContentMapper;
import fr.cnam.projetSiw.client.core.mapper.MainContentMapper;
import fr.cnam.projetSiw.client.core.mapper.MenuMapper;
import fr.cnam.projetSiw.client.core.mapper.SuggestContentMapper;
import fr.cnam.projetSiw.client.core.mapper.TopContentMapper;
import fr.cnam.projetSiw.client.ressources.GeneralBundle;
import fr.cnam.projetSiw.client.structure.HomePlace;
import fr.cnam.projetSiw.widget.ressources.WidgetBundle;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GeoZic implements EntryPoint {


	/**
	 * This is the entry point method.
	 */
	private Place defaultPlace = new HomePlace("Accueil");
	
	private static boolean doOnce = false;

    public void onModuleLoad()
    {
    	
    	
    	
    	
        ClientFactory clientFactory = GWT.create(ClientFactory.class);
        EventBus eventBus = clientFactory.getEventBus();
        PlaceController placeController = clientFactory.getPlaceController();

        
        //init Area
        DisplayArea display = new DisplayArea();
        
        new ActivityManager(new MenuMapper(clientFactory), eventBus).setDisplay(display.getMenuArea());
        new ActivityManager(new TopContentMapper(clientFactory), eventBus).setDisplay(display.getTopContentArea());
        new ActivityManager(new MainContentMapper(clientFactory), eventBus).setDisplay(display.getMiddleContentArea());
		new ActivityManager(new SuggestContentMapper(clientFactory), eventBus).setDisplay(display.getSuggestContent());
		new ActivityManager(new BotLeftContentMapper(clientFactory), eventBus).setDisplay(display.getBotLeftContent());
		new ActivityManager(new BotMidContentMapper(clientFactory), eventBus).setDisplay(display.getBotMidContent());
		new ActivityManager(new BotRightContentMapper(clientFactory), eventBus).setDisplay(display.getBotRightContent());
        
        

        // Start PlaceHistoryHandler with our PlaceHistoryMapper
        AppPlaceHistoryMapper historyMapper= GWT.create(AppPlaceHistoryMapper.class);
        PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
        historyHandler.register(placeController, eventBus, defaultPlace);
        
        injectCss();
        
        

        RootPanel.get().add(display);
       
		// On geolocalise l'user au démarrage de l'application
        GeolocationManager.getInstance().searchCurrentPosition(historyHandler);
    }

	private void injectCss()
	{
		GeneralBundle.INSTANCE.css().ensureInjected();
		WidgetBundle.INSTANCE.cssGrid().ensureInjected();
		WidgetBundle.INSTANCE.cssSquare().ensureInjected();
	}
	
	public static void handleHistory(PlaceHistoryHandler historyHandler)
	{
		if (!doOnce)
		{
			doOnce = true;
			historyHandler.handleCurrentHistory();
		}
	}
}
